home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Images and Bitmaps / ScribbleWithBitmap / ScribbleWithBitmap.cs next >
Encoding:
Text File  |  2001-01-15  |  1.9 KB  |  75 lines

  1. //-------------------------------------------------
  2. // ScribbleWithBitmap.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class ScribbleWithBitmap: Form
  9. {
  10.      bool     bTracking;
  11.      Point    ptLast;
  12.      Bitmap   bitmap;
  13.      Graphics grfxBm;
  14.  
  15.      public static void Main()
  16.      {
  17.           Application.Run(new ScribbleWithBitmap());
  18.      }
  19.      public ScribbleWithBitmap()
  20.      {
  21.           Text = "Scribble with Bitmap";
  22.           BackColor = SystemColors.Window;
  23.           ForeColor = SystemColors.WindowText;
  24.  
  25.                // Create bitmap
  26.  
  27.           Size size = SystemInformation.PrimaryMonitorMaximizedWindowSize;
  28.           bitmap = new Bitmap(size.Width, size.Height);
  29.  
  30.                // Create Graphics object from bitmap
  31.  
  32.           grfxBm = Graphics.FromImage(bitmap);
  33.           grfxBm.Clear(BackColor);
  34.      }
  35.      protected override void OnMouseDown(MouseEventArgs mea)
  36.      {
  37.           if (mea.Button != MouseButtons.Left)
  38.                return;
  39.  
  40.           ptLast = new Point(mea.X, mea.Y);
  41.           bTracking = true;
  42.      }
  43.      protected override void OnMouseMove(MouseEventArgs mea)
  44.      {
  45.           if (!bTracking)
  46.                return;
  47.  
  48.           Point ptNew = new Point(mea.X, mea.Y);
  49.           
  50.           Pen pen = new Pen(ForeColor);
  51.           Graphics grfx = CreateGraphics();
  52.           grfx.DrawLine(pen, ptLast, ptNew);
  53.           grfx.Dispose();
  54.  
  55.                // Draw on bitmap
  56.  
  57.           grfxBm.DrawLine(pen, ptLast, ptNew);
  58.  
  59.           ptLast = ptNew;
  60.      }
  61.      protected override void OnMouseUp(MouseEventArgs mea)
  62.      {
  63.           bTracking = false;
  64.      }
  65.      protected override void OnPaint(PaintEventArgs pea)
  66.      {
  67.           Graphics grfx = pea.Graphics;
  68.  
  69.                // Display bitmap
  70.  
  71.           grfx.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
  72.      }
  73. }
  74.  
  75.